<?php


/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
*/


// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();


jimport('joomla.application.component.model');


class GalleryModelImages extends JModel
{
	function __construct()
	{
		parent::__construct();

		global $mainframe, $option;

		// Get the pagination request variables.
		$limit      = $mainframe->getUserStateFromRequest('global.list.limit',  'limit',      $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest("$option.limitstart", 'limitstart', 0,                                'int');

		$this->setState('limit',      $limit);
		$this->setState('limitstart', $limitstart);
	}

	function getData()
	{
		if (null === $this->_data)
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
			if (null === $this->_data)
			{
				$this->setError($this->_db->getErrorMsg());
				return null;
			}
		}
		return $this->_data;
	}

	function getTotal()
	{
		if (null === $this->_total)
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}
		return $this->_total;
	}

	function getPagination()
	{
		if (null === $this->_pagination)
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_pagination;
	}

	function _buildQuery()
	{
		$where   = $this->_buildQueryWhere();
		$orderBy = $this->_buildQueryOrderBy();

		$query = 'SELECT i.*, c.title as category, u.name as editor'
			. "\n	FROM #__gallery_images i"
			. "\n		INNER JOIN #__categories c ON c.id = i.catid"
			. "\n		INNER JOIN #__users u ON u.id = i.checked_out"
			. $where
			. $orderBy
			;
		return $query;
	}

	function _buildQueryWhere()
	{
		global $mainframe, $option;

		$filter_state       = $mainframe->getUserStateFromRequest($option.'filter_state',       'filter_state',     '',           'word');
		$filter_catid       = $mainframe->getUserStateFromRequest($option.'filter_catid',       'filter_catid',     0,            'int');
		$filter_order       = $mainframe->getUserStateFromRequest($option.'filter_order',       'filter_order',     'i.ordering', 'cmd');
		$filter_order_Dir   = $mainframe->getUserStateFromRequest($option.'filter_order_Dir',   'filter_order_Dir', '',           'word');
		$search             = $mainframe->getUserStateFromRequest($option.'search',             'search',           '',           'string');
		$search             = JString::strtolower($search);

		$where = array();

		if ($filter_catid > 0)
			$where[] = 'i.catid = '.(int) $filter_catid;

		if ($search)
		{
			$term = $this->_db->Quote("%$search%");
			$where[] = "(LOWER(i.title) LIKE $term OR LOWER(i.filename) LIKE $term)";
		}

		if ($filter_state)
		{
			if ($filter_state == 'P')
				$where[] = 'i.published = 1';
			else if ($filter_state == 'U')
				$where[] = 'i.published = 0';
		}

		$where  = count($where) ? "\n	WHERE " . implode(' AND ', $where) : '';

		return $where;
	}

	function _buildQueryOrderBy()
	{
		global $mainframe, $option;

		$filter_order       = $mainframe->getUserStateFromRequest($option.'filter_order',       'filter_order',     'i.ordering',  'cmd');
		$filter_order_Dir   = $mainframe->getUserStateFromRequest($option.'filter_order_Dir',   'filter_order_Dir', '',         'word');

		if ($filter_order == 'i.title')
			$orderBy 	= "\n	ORDER BY category, i.title $filter_order_Dir";
		else
			$orderBy 	= "\n	ORDER BY $filter_order $filter_order_Dir, category, i.ordering";

		return $orderBy;
	}

	var $_data = null;

	var $_total = null;

	var $_pagination = null;
}


?>